$(function () {

    /**
     *  解析url  的参数
     */
    function queryURLparams() {
        var url = window.location.href;
        var obj = {}
        if (url.indexOf('?') < 0) return obj
        var arr = url.split('?')
        url = arr[1]
        var array = url.split('&')
        for (var i = 0; i < array.length; i++) {
            var arr2 = array[i]
            var arr3 = arr2.split('=')
            obj[arr3[0]] = arr3[1]
        }
        console.log(obj);
        return obj
    }

    var data = queryURLparams(url);
    vm.laboratory.id = data.id;
    vm.getLaboratory();
    vm.lb=data.id?true:false;


    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/jxcomputydevice/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '电脑名称', name: 'deviceName', index: 'device_name', width: 80},
            {label: '电脑编号', name: 'num', index: 'num', width: 80},
            {label: '操作', name: 'id', index: 'id', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                var useStatus=rowObject.useStatus;
                  if(useStatus!="2"){
                      return "<a class='btn btn-default' onclick='sendfault(" + cellvalue + ")' ><i class='fa fa-wrench' style='font-size: 20px'></i>&nbsp;故障保修</a>";
                  }else{
                      return "<a class='btn btn-default' onclick='sendfault("  + cellvalue + ")' ><i class='fa fa-desktop' style='font-size: 20px'></i>&nbsp;重置状态</a>";
                  }

                }},
            {
                label: '实验室', name: 'laboratoryId', index: 'laboratory_id', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    console.log(rowObject);
                    var num = rowObject.num;
                    return "实验室" + rowObject.laboratoryName + "/" + rowObject.laboratoryNum;
                },
            },
            {label: '公网Ip', name: 'internetIp', index: 'internet_ip', width: 80},
            {label: '电脑ip', name: 'ip', index: 'ip', width: 80},
            {label: '端口号', name: 'port', index: 'port', width: 80},
            {
                label: '使用状态',
                name: 'useStatus',
                index: 'use_status',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    if(cellvalue == "2"){
                        return "<span style='color: #9f191f'> 故障中</span>"
                    }else if(cellvalue == "0"){
                        return " 未使用"
                    }else {
                        return " 正在使用！"
                    }

                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        shrinkToFit:true,
        autoScroll: true,
        pager: "#jqGridPager",
        postData: {'laboratoryId': data.id},
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
           // $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "scroll"});
        }
    });


});

/**
 *  解析url  的参数
 */
function queryURLparams(url) {
    var obj = {}
    if (url.indexOf('?') < 0) return obj
    var arr = url.split('?')
    url = arr[1]
    var array = url.split('&')
    for (var i = 0; i < array.length; i++) {
        var arr2 = array[i]
        var arr3 = arr2.split('=')
        obj[arr3[0]] = arr3[1]
    }
    return obj
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            deviceName: null,
            num: null
        },
        lb:true,
        showList: true,
        title: null,
        laboratory: {}, //  当前实验室的id
        jxComputyDevice: {}
    },
    methods: {
        closeAll:function(){   //  关闭所有
            console.log("执行关闭");
            var  url="sys/screenControl/closeAll";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                success: function(r){
                    if(r.code === 0){
                        layer.msg("执行成功！")
                        vm.reload();
                    }
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.jxComputyDevice = {laboratoryId: vm.laboratory.id, useStatus: 0};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.jxComputyDevice.id == null ? "sys/jxcomputydevice/save" : "sys/jxcomputydevice/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.jxComputyDevice),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/jxcomputydevice/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/jxcomputydevice/info/" + id, function (r) {
                vm.jxComputyDevice = r.jxComputyDevice;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'deviceName': vm.q.deviceName, 'num': vm.q.num, 'laboratoryId': vm.laboratory.id},
                page: page
            }).trigger("reloadGrid");
        }, getLaboratory: function () {   // 拿到当前实验室
            $.get(baseURL + "sys/jxlaboratory/info/" + vm.laboratory.id , function (r) {
                vm.laboratory = r.jxLaboratory;
                $("#lab_num").text(vm.laboratory.name);
            });
        }

    }
});


 function  sendfault(id){   //  故障报修
     $.get(baseURL + "sys/jxcomputydevice/sendfault/" + id, function (r) {
        layer.msg("已更新设备状态");
        vm.reload();
     });

}